#!/usr/bin/perl -w
####################################################
# Copyright (c) 2002-2007 BioWare Corp.
# http://www.bigdaddymonitor.org
#
# This file is part of the BigDaddy Monitoring
# program. It is free software distributed under
# the terms of the BSD License. It is ditributed
# in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without event the implied
# warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
# The BSD License is available from:
# http://www.opensource.org/licenses/bsd-license.php
####################################################
#=============================================================================#
#BigDaddy Module: Ping (ICMP) Check
#Author: Duleepa Wijayawardhana
#Date: 26.03.2004
#=============================================================================#

use strict;
use IO::Socket;

my ($server) = $ARGV[0];
my $port_check = $ARGV[1];
my $connected=0;
my $fail=0;
my $failCount;

for (my $j=0; $j<6; $j++)
{
    $fail=0;

    my $checkport = IO::Socket::INET->new(
        PeerAddr => "$server",
        PeerPort => $port_check,
        Proto => "tcp",
        Timeout => '1') or $fail = 1;

    if ($fail==1)
    {
        $failCount++;
    }
    else
    {
        $failCount=0;
    }
}

if ($failCount>3)
{
    print "0";
}
else
{
    print "1";
}
